package com.wmlt.dao.impl;

import com.wmlt.dao.LiveBroadcastDao;
import com.wmlt.entity.LiveBroadcast;
import com.wmlt.entity.User;
import com.wmlt.util.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LiveBroadcastDaoImpl extends JdbcUtil implements LiveBroadcastDao {
    LiveBroadcast liveBroadcast=null;
    String sql = "";
    int result = 0;
    Connection connection = null;
    PreparedStatement prea = null;
    ResultSet res = null;
    List<LiveBroadcast> liveBroadcastList=null;
    /**
     * @description: 从数据中查询用户的key是否存在
     * @param:  直播key
     * @return:
     * @author liyuxiang
     * @date: 2021/4/7 8:51 下午
     */
    @Override
    public LiveBroadcast getLiveBroadcastByKey(String key) {
        connection = JdbcUtil.getConnection();
        try {
            sql = "select * from live_broadcast where live_key=?";
            prea = connection.prepareStatement(sql);
            prea.setString(1, key);
            res = prea.executeQuery();
            while (res.next()) {
                liveBroadcast=new LiveBroadcast(res.getLong("id"),res.getInt("live_no"),res.getInt("cid"),res.getString("title"),res.getString("live_image_url"),res.getString("live_key"), res.getString("live_start_time"),res.getString("live_end_time"),res.getInt("status"),res.getInt("deleted"),res.getString("user_id"));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(res, prea, connection);
        }
        return liveBroadcast;
    }

//    @Override
//    public int deleteLiveBroadcastByKey(String key) {
//        try {
//            sql = "delete  from live_broadcast where live_key=?";
//            connection = JdbcUtil.getConnection();
//            prea = connection.prepareStatement(sql);
//            prea.setString(1, key);
//            result=prea.executeUpdate();
//        } catch (SQLException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        } finally {
//            JdbcUtil.close(null, prea, null);
//        }
//        return result;
//    }

    @Override
    public int updateLiveState(String key,int status) {
        connection = JdbcUtil.getConnection();
        try {
            sql = "update live_broadcast set status=? where live_key=?";
            prea = connection.prepareStatement(sql);
            prea.setInt(1, status);
            prea.setString(2, key);
            result=prea.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JdbcUtil.close(null, prea, null);
        }
        return result;
    }

    @Override
    public List<LiveBroadcast> getLiveBroadcastList() {
        connection = JdbcUtil.getConnection();
        try {

//            if (liveTitle==null||liveTitle.equals("")){
//                sql = "select * from live_broadcast where title=?";
//                prea.setString(1, liveTitle);
//            }else {
//                sql = "select * from live_broadcast";
//            }
            sql = "SELECT * FROM `live_broadcast` as lb,User as u where u.user_id=lb.user_id LIMIT 0,5";
            prea = connection.prepareStatement(sql);
            res = prea.executeQuery();
            if (res!=null){
                liveBroadcastList=new ArrayList<LiveBroadcast>();
            }
            while (res.next()) {
                User user=new User(res.getString(22), res.getString(13), null, res.getString(15), res.getString(23), res.getString(29), res.getString(30), res.getString(27), res.getString(24), res.getDouble(17));
                System.out.println(user);


                liveBroadcast=new LiveBroadcast(res.getLong(1),res.getInt(2),res.getInt(3),res.getString(4),res.getString(5),res.getString(6), res.getString(7),res.getString(8),res.getInt(9),res.getInt(10),res.getString(11));
                liveBroadcast.setUser(user);
                liveBroadcastList.add(liveBroadcast);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(res, prea, connection);
        }
        return liveBroadcastList;
    }

    @Override
    public int auditLiveBroadcast(int[] liveNo) {
//        try {
//            sql = "update live_broadcast set status=? where live_no=?";
//            connection = JdbcUtil.getConnection();
//            prea = connection.prepareStatement(sql);
//            prea.setInt(1, 1);
//            prea.setInt(2, liveNo);
//            result=prea.executeUpdate();
//        } catch (SQLException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        } finally {
//            JdbcUtil.close(null, prea, null);
//        }
        sql = "update live_broadcast set status=1 where live_no in (?";
        StringBuffer sqlbuf = new StringBuffer(sql);
        connection = JdbcUtil.getConnection();
        try {
            //拼接sqlbuf，两种情况
            if (liveNo.length == 1) {
                //有1条时， 直接加 "）"结束
                sqlbuf.append(")");
            } else {
                //有多条时候，拼接length-1 次，因为初始sql语句已经有一次了。"delete  from `user` where u_id in (? "
                for (int i = 0; i < liveNo.length - 1; i++) {
                    sqlbuf.append(",?");

                }
                //最后拼接" )"  圆满完成拼接sql
                sqlbuf.append(")");
            }
            System.out.println(sqlbuf.toString());
            prea = connection.prepareStatement(sqlbuf.toString());
            if (liveNo.length == 1) {
                prea.setInt(1, liveNo[0]);
            } else {

                for (int i = 1; i <= liveNo.length; i++) {
                    prea.setInt(i, liveNo[i - 1]);
                    //userids[i-1] 因为数据下标从0 开始。
                }
            }
            result=prea.executeUpdate();

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JdbcUtil.close(null, prea, connection);
        }
        return result;
    }
    //根据key拿到直播信息

    @Override
    public List<LiveBroadcast> getUserInfoByLiveKey(String liveKey) {
        connection = JdbcUtil.getConnection();
        try {

//            if (liveTitle==null||liveTitle.equals("")){
//                sql = "select * from live_broadcast where title=?";
//                prea.setString(1, liveTitle);
//            }else {
//                sql = "select * from live_broadcast";
//            }
            sql = "SELECT * FROM `live_broadcast` as lb,User as u where u.user_id=lb.user_id and lb.live_key=?";

            prea = connection.prepareStatement(sql);
            prea.setString(1,liveKey);
            res = prea.executeQuery();
            if (res!=null){
                liveBroadcastList=new ArrayList<LiveBroadcast>();
            }
            while (res.next()) {
                User user=new User(res.getString(22), res.getString(13), null, res.getString(15), res.getString(23), res.getString(29), res.getString(30), res.getString(27), res.getString(24), res.getDouble(17));
                System.out.println(user);


                liveBroadcast=new LiveBroadcast(res.getLong(1),res.getInt(2),res.getInt(3),res.getString(4),res.getString(5),res.getString(6), res.getString(7),res.getString(8),res.getInt(9),res.getInt(10),res.getString(11));
                liveBroadcast.setUser(user);
                liveBroadcastList.add(liveBroadcast);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(res, prea, connection);
        }
        return liveBroadcastList;
    }

     //根据用户ID获取直播状态

    @Override
    public int getuUserLiveBroadcastStautsByid(String userId) {
        connection = JdbcUtil.getConnection();
        try {
            sql = "SELECT  `status` FROM live_broadcast WHERE user_id=?";
            prea = connection.prepareStatement(sql);
            prea.setString(1, userId);
            res = prea.executeQuery();
            while (res.next()){
                result= res.getInt("status");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(res, prea, null);
        }
        return result;
    }



    //处理用户申请直播请求

    @Override
    public int handlingLivestreamingRequests(String userid) {
        connection = JdbcUtil.getConnection();
        try {
            sql = "INSERT INTO live_broadcast VALUES( NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,?)";
            prea = connection.prepareStatement(sql);
            prea.setString(1, userid);
            result=prea.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(res, prea, null);
        }
        return result;
    }

    @Override
    public LiveBroadcast getLiveBroadcastInfoByUserId(String userId) {
        connection = JdbcUtil.getConnection();
        try {
            sql = "SELECT  * FROM live_broadcast WHERE user_id=?";
            prea = connection.prepareStatement(sql);
            prea.setString(1, userId);
            res = prea.executeQuery();
            while (res.next()){
                liveBroadcast=new LiveBroadcast(res.getLong("id"),res.getInt("live_no"),res.getInt("cid"),res.getString("title"),res.getString("live_image_url"),res.getString("live_key"), res.getString("live_start_time"),res.getString("live_end_time"),res.getInt("status"),res.getInt("deleted"),res.getString("user_id"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(res, prea, connection);
        }
        return liveBroadcast;
    }

    @Override
    public int launchLiveBroadcast(LiveBroadcast liveBroadcast) {
        System.out.println(liveBroadcast);
        connection = JdbcUtil.getConnection();
        try {
            sql = "update live_broadcast set title=?,live_image_url=?,status=? where user_id=?";
            
            prea = connection.prepareStatement(sql);
            prea.setString(1,liveBroadcast.getTitle());
            prea.setString(2,liveBroadcast.getLiveImageUrl());
            prea.setInt(3,liveBroadcast.getStatus());
            prea.setString(4,liveBroadcast.getUserId());
            result=prea.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JdbcUtil.close(null, prea, null);
        }
        return result;
    }

    @Override
    public int applyforLive(LiveBroadcast liveBroadcast) {
        connection = JdbcUtil.getConnection();
        try {
            sql = "INSERT INTO live_broadcast VALUES( ?,1,null,null,null,null,null,null,0,NULL,?)";
            prea = connection.prepareStatement(sql);
            prea.setInt(1, liveBroadcast.getLiveNo());
            prea.setString(2,liveBroadcast.getUserId());
            result=prea.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(res, prea, null);
        }
        return result;
    }

}